package com.rust.sip;

import android.Manifest;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.SurfaceView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.PermissionChecker;

import com.autulin.gb28181library.MediaOutput;
import com.autulin.gb28181library.MediaRecorderBase;
import com.autulin.gb28181library.MediaRecorderNative;
import com.rust.sip.GB28181.GB28181CallBack;

public class MainActivity extends AppCompatActivity implements
        MediaRecorderBase.OnErrorListener, MediaRecorderBase.OnPreparedListener {
    public MyService mService;
    private ServiceConnection mConnection;
    private MediaRecorderNative mMediaRecorder;
    private MediaOutput mediaOutput;
    private SurfaceView mSurfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //region 权限申请
        if (ContextCompat.checkSelfPermission(this.getApplicationContext(), Manifest.permission.CAMERA) == PermissionChecker.PERMISSION_DENIED) {

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
        }
        //endregion

        MediaRecorderBase.QUEUE_MAX_SIZE = 20;
        Log.e("debug", "SMALL_VIDEO_HEIGHT: " + MediaRecorderBase.SMALL_VIDEO_HEIGHT + ", SMALL_VIDEO_WIDTH:" + MediaRecorderBase.SMALL_VIDEO_WIDTH);

        mSurfaceView = findViewById(R.id.record_preview);
        mMediaRecorder = new MediaRecorderNative();
        mMediaRecorder.setOnErrorListener(this);
        mMediaRecorder.setOnPreparedListener(this);
        mMediaRecorder.setSurfaceHolder(mSurfaceView.getHolder());
        mMediaRecorder.prepare();

        //region GB service
        mConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                MyService.MyBinder mBinder = (MyService.MyBinder) service;
                mService = mBinder.getService();
                if (mService != null) {
                    mService.setGB28181CallBack(new GB28181CallBack() {
                        @Override
                        public void onStartRtp(int ssrc, String url, int port, int is_udp) {
                            //udp
                            if (is_udp == 0) {
                                mMediaRecorder.setUdpOutPut(url, port, ssrc);
                            } else {
                                mMediaRecorder.setTcpOutPut(url, port, 8088, ssrc);
                            }
                            Log.e("debug", "开始RTP推流: " + url + ":" + port + "  ssrc:" + ssrc + "  is_udp:" + is_udp);
                            mMediaRecorder.startMux();
                        }

                        @Override
                        public void onStopRtp(int ssrc) {
                            Log.e("debug", "onStopRtp ssrc:" + ssrc);
                            mMediaRecorder.endMux();
                        }
                    });
                    mService.GB28181Init();
                    mService.GB28181_Start();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                mService = null;
            }
        };
        Intent mServiceIntent = new Intent(this, MyService.class);
        bindService(mServiceIntent, mConnection, Service.BIND_AUTO_CREATE);
        //endregion
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mMediaRecorder != null) mMediaRecorder.endMux();
        unbindService(mConnection);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            for (int result : grantResults) {
                if (result != PermissionChecker.PERMISSION_GRANTED) {
                    this.finish();
                }
            }
        }
    }

    @Override
    public void onPrepared() {
        Log.e("debug", "onPrepared");
    }

    @Override
    public void onVideoError(int what, int extra) {
        Log.e("debug", "onVideoError what:" + what + "  extra:" + extra);
    }

    @Override
    public void onAudioError(int what, String message) {
        Log.e("debug", "onAudioError what:" + what + "  message:" + message);
    }
}
